import Taro, { Component } from '@tarojs/taro'
import { Swiper, SwiperItem, Image, View, Text } from '@tarojs/components'
import Goodshearder from './goodshearder'
import '../../assets/common.scss'
import CommonJs from '../../utils/common'
import '../my/unpaid.scss'
import './cloud.scss'

export default class Index extends Component {
  constructor(props) {
  	super(props);
    this.state = {
    	goodslist:[],
    	cpcodelist:[],
    	cpcodearr:[],
    	wlcode:0,
    	mailNo:'',
    	cpindex:'',
    	wlindex:'',
      pageheight:0,
      userid:Taro.getStorageSync('dateinfo').userid,
	  	pageNumber:0,//页码数
      scrollPd:false,//是否可滑动
      hasNextPage:false,//是否有下一页
    }
  }
  config = {
    navigationBarTitleText: '销售订单'
  }

  componentWillMount () {
  	var _this=this
  	Taro.getSystemInfo({
      success:function(res){
        _this.setState({
          pageheight: res.windowHeight
        })
      }
    })
		this.getdata(0)
		this.getcpcode()
  }
  componentDidMount () {}
  componentDidShow () {}
  getdata(pageNumber){
  	 var _this=this;
  	 var dateinfo = Taro.getStorageSync('dateinfo');
  	 var url='/yunwarehouse/papi/v1.9/ticket?page='+pageNumber+"&toFromDealerId.equals="+dateinfo.userid;
    CommonJs.ajaxRequest(url,'','GET',function(res){
		if(res.data.page<res.data.totalPages){
			var hasNextPage = true
		 }
		 else{
			var hasNextPage = false
		 }
		  var results = _this.state.goodslist
		 
		  var result = res.data.list;
		  for(var i = 0; i < result.length; i++){
			results.push(result[i])
		  }
		   
		  if (hasNextPage) {
			_this.setState({
			  goodslist:results,
			  pageNumber: res.data.page,
			  hasNextPage: hasNextPage,
			  scrollPd: true
			})
		  }
		  else {
			_this.setState({
			  goodslist:results,
			  pageNumber: res.data.page,
			  hasNextPage: hasNextPage,
			  scrollPd: false
			})
		  }

    },'wfw')
  }
  loadMore(){
    var _this = this;
    var pd = _this.state.scrollPd;
    if(pd) {
      //防止多次触发滑动到底部事件
      _this.setState({
        scrollPd: false//禁止下拉
      })
      var pageNumber = _this.state.pageNumber + 1;
      _this.getunpaid(pageNumber)
    }else{
      var hasNextPage =  _this.state.hasNextPage;
      if(hasNextPage){
        Taro.showToast({
            title:'正在加载数据...',
            icon: 'none',
            duration: 2000
          });
      }else{
        Taro.showToast({
            title:'没有更多数据啦！',
            icon: 'none',
            duration: 2000
          });
      }
    }
  }
   ordertype(val){
	  if(val=='STAY'){
		  return '待发货'
	  }
	  else if(val=='APPLY'){
	  	
		  return '待发货'
	  }
	  else if(val=='COMPLETE'){
		  return '发货完成'
	  }
	  else{
		  return ''
	  }
  }
  open(url){
    CommonJs.openTo(url);
  }
  dofh(index){
  	var goodslist=this.state.goodslist
  	var url='/yunwarehouse/papi/v1.9/ticket/dealerDelivery'
  	var dates={
  		"ticketId":goodslist[index].id
  	}
		var _this=this;
	    CommonJs.ajaxRequest(url,dates,'PUT',function(res){
	    	Taro.showToast({
				  title:'操作成功',
				  icon: 'none',
				  duration: 2000
				})
	    	setTimeout(function(){
	    		_this.getdata(0)
	    	},2000)
	    },'wfw')
  }
  wledit(index){
  	var result = this.state.goodslist
  	var cpcodelist=this.state.cpcodelist
  	var cindex=-1;
  	for(var i=0;i<cpcodelist.length;i++){
  		if(cpcodelist[i].cpCode==result[index].cpCode){
  			cindex=i;
  		}
  	}
  	if(cindex>=0){
  		this.setState({
				wlcode:1,
				wlindex:index,
				cpindex:cindex,
				mailNo:result[index].mailNo
			})
  	}
  	else{
			this.setState({
				wlcode:1,
				wlindex:index,
				cpindex:'',
				mailNo:''
			})
		}
  }
  closewledit(){
  	this.setState({
			wlcode:0
		})
  }
  wlinfo(id){
  	this.open("/pages/my/wlinfo?id="+id+"&type=2")
  }
  getcpcode(){
  	var url='/order/papi/v1.9/logistics/cpcode'
  	var _this=this;
    CommonJs.ajaxRequest(url,'','GET',function(res){
    	var arr=[]
  		var list=res.data;
	  	for(var i=0;i<list.length;i++){
	  		arr.push(list[i].name)
	  	}
  		_this.setState({
			  cpcodelist:res.data,
			  cpcodearr:arr
			})
    	
    },'wfw')
  
  }
  handleChange(name,e){
    var _this = this
  	let value=e.target.value;
  	this.setState({
      [name]: value
    });
  }
  changecpcode(e){
  	var index=e.detail.value;
  	this.setState({
			cpindex:index
		})
  }
  wlsure(){
  	var result = this.state.goodslist
  	var cpcodearr=this.state.cpcodearr
    var cpcodelist=this.state.cpcodelist
    var cpindex=this.state.cpindex
    var wlindex=this.state.wlindex 
  	var url='/order/papi/v1.9/logistics/addOrderLpcPackPub'
  	var dates={
		  "cpCode": cpcodelist[cpindex].cpCode,
		  "entityType": 2,
		  "mailNo": this.state.mailNo,
		  "orderId": result[wlindex].id
		}
  	var _this=this;
    CommonJs.ajaxRequest(url,dates,'POST',function(res){
    	CommonJs.msg('操作成功');
    	_this.closewledit()
    },'wfw')
  }
    render () {
    const result = this.state.goodslist
    const cpcodearr=this.state.cpcodearr
    const cpcodelist=this.state.cpcodelist
    const cpindex=this.state.cpindex
    return (
      <View className='page'>
      
        <ScrollView className='qun-goods' scrollY='true' style={'height:'+this.state.pageheight+'px;'} onScrollToLower={this.loadMore.bind(this,'')}>
        <View className='qun-znwn' style={result.length!=0?'display:none':'display:block;'}>暂无订单</View>
        {result.map((item, index) => (
        <View className='goodsorder-content'>
          <View className='goodsorder-unpaid'>
            <View className='unpaid-title'>
              <View className='qun-scallty'>
				<View className='unpaid-state'>{this.ordertype(item.sentStatus,item.fromDealerId)}</View>
                <Image mode='scaleToFill' className='unpaid-photo' src='https://minichat.g2ma.com/kedouimg/store.png'></Image>
                <View className='unpaid-shopname'><Text className='unpaid-tel'>{item.fromShopName}</Text>({item.serialNo})</View>
              </View>
            </View>
            <View className='unpaid-content'>
              {item.goodsNoTicketVMList.map((item1, index1) => (
              <View className='unpaid-goods'>
							<View className='unpaid-pic'>
								<Image mode='scaleToFill' className='unpaid-pic-img' src={item1.itemPicture?item1.itemPicture:'https://minichat.g2ma.com/kedouimg/A6984900727459logo.png'}></Image>
			                </View>
							<View className='unpaid-right'>
			                  <View className='unpaid-goodsname'>{item1.itemName}</View>
			                  <View className='unpaid-specifications'>规格：{item1.itemSku}</View>
			                  <View className='unpaid-total'>
			                  	<View className='unpaid-num' style={item.type=='PURCHASE'?'':'display:none'}>采购商品</View>
			                    <View className='unpaid-num'>数量：{item1.amount}</View>
			                    <View className='unpaid-price'>{item1.price/100}{this.state.malltype?this.state.malltype:'元'}</View>
			                    
			                  </View>
			                </View>
			              </View>
						  ))}
              
              <View className='unpaid-postage'>总数量：{item.goodsAmount}
              <View className='unpaid-time' style='float:left;text-aling:left;'>{CommonJs.FormatDate(item.createAt,3)}</View>
              </View>

            </View>
            <View className='unpaid-mes'>
              <View className='unpaid-people'>收货人：{item.receiveName}  {item.receiveMobile}</View>
              <View className='unpaid-address'>收货地址：{item.regionName}{item.address}</View>
              <View className='unpaid-address' style={item.orderSerialNo?'':'display:none'}>订单流水：{item.orderSerialNo}</View>
            </View>
          	<View className='unpaid-order'>
              <Text className='unpaid-cancel' style={item.ticketApplyType==2&&item.sentStatus=='APPLY'?'':'display:none'} onClick={this.dofh.bind(this,index)}>确认发货</Text>
              <Text className='unpaid-cancel' style={item.ticketApplyType==2||item.ticketApplyType==3?'':'display:none'} onClick={this.wledit.bind(this,index)}>添加/修改物流信息</Text>
              <Text className='unpaid-cancel' style={item.sentStatus=='COMPLETE'||item.sentStatus=='GOODS_RECEIVED'?'':'display:none'}  onClick={this.wlinfo.bind(this,item.id)}>查看物流信息</Text>
            </View>
            
             
          </View>
        </View>
        ))}
        </ScrollView>
        <View className='wladd' style={this.state.wlcode==1?'':'display:none'}>
        	<View className='wladd-con'>
        		<View className='wladd-tit'>添加/修改物流</View>
        		<View className='wladd-main'>
        			 <View className='page-shop' >
			          <Picker mode='selector'  range={cpcodearr}  onChange={this.changecpcode.bind(this)}>
			              <View className='shop-type'>
			                <Text>选择快递公司</Text>
			              </View>
			              <View className='shop-types'>
			                	{cpindex?cpcodelist[cpindex].name:'请选择'}
			              </View>
			           </Picker>
			         </View>
        			 <View className='page-shop'>
			          <View className='shop-type'>
			              <Text>快递单号</Text>
			          </View>
			          <View className='shop-input'>
			            <Input type='text' placeholder='输入快递单号' value={this.state.mailNo} focus onInput={this.handleChange.bind(this,'mailNo')}/>
			          </View>
			         </View>
			         <View className='ptc-btn'>
	        			<View className='btn-cancle' onClick={this.closewledit.bind(this)}>取消</View>
	        			<View className='btn-sure' onClick={this.wlsure.bind(this)}>确定</View>
	        		</View>
        		</View>
        	</View>
        </View>
      </View>
    )
  }
}